<template>
  <el-dialog
    title="新增权限"
    width="50%"
    :visible.sync="visible"
    :before-close="hanlerClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="form.name" placeholder="请输入权限名称"></el-input>
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="form.code" placeholder="请输入权限标识"></el-input>
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input
          v-model="form.description"
          placeholder="请输入权限描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限开关">
        <!-- 设置active-value和inactive-value属性，
        扩展el-switch双向绑定的值，
        接受Boolean, String或Number类型的值 -->
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="hanlerClose">取 消</el-button>
      <el-button type="primary" @click="onConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPmsApi, getPmsDetailApi, editPmsApi } from '@/api/permission'
export default {
  props: {
    visible: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      form: {
        enVisible: '',
        name: '',
        code: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      },
      type: '1', // type=1代表菜单权限；type=2代表按钮权限
      pid: '0' // 父级id，菜单权限为一级默认pid为’0‘
    }
  },
  methods: {
    // 获取权限详情数据
    async getPmsInfo(id) {
      this.form = await getPmsDetailApi(id)
    },
    // 处理弹框关闭逻辑
    hanlerClose() {
      this.form = {
        enVisible: '',
        name: '',
        code: '',
        description: ''
      }
      this.type = '1'
      this.pid = '0'
      this.$emit('update:visible', false)
    },
    // 点击确认按钮
    async onConfirm() {
      await this.$refs.form.validate()
      if (this.form.id) {
        // 编辑状态
        await editPmsApi(this.form)
      } else {
        // 新增
        await addPmsApi({ ...this.form, pid: this.pid, type: this.type })
      }
      this.$message.success('操作成功')
      this.hanlerClose()
      this.$emit('success')
    }
  }
}
</script>

<style>
</style>